package ooday04;

import com.sun.org.apache.bcel.internal.generic.NEW;

/** 多态的演示 */
public class PolymorphicDemo {


    public static void main(String[] args) {



        //演示墙砖的成功的条件
        /*

        Animal o = new Dog("大黄",2,"土黄");
        Dog g = (Dog) o;//引用o所指的对象, 就是Dog类型
        Swim s = (Swim) o;//引用o所指向的对象, 实现了Swim接口
        //Fish f = (Fish)o;//运行时会发生ClassCastException类型转换异常
        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false
        if (o instanceof Fish){//false
            Fish f = (Fish) o;
        }

         */



        Animal[] animals = new Animal[5];
        animals[0] = new Dog("大黄",2,"土黄");
        animals[1] = new Dog("大白",3,"白");
        animals[2] = new Fish("金鱼",3,"黄");
        animals[3] = new Fish("白鱼",1,"白");
        animals[4] = new Chick("母鸡",2,"花");

        for(int i = 0; i < animals.length; i++) {//遍历所有动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].drink();//每个动物喝水
            animals[i].eat();//每个动物吃饭

            if (animals[i] instanceof Dog) {//布尔判断
                Dog dog = (Dog) animals[i];//狗狗类的强转
            }
            if (animals[i] instanceof Chick) {//布尔判断
                Chick chick = (Chick) animals[i];//小鸡类的强转
            }
            if(animals[i] instanceof Swim){//布尔判断
                Swim s = (Swim) animals[i];//游泳接口
                s.swim();
            }
        }
        //演示向上造型(多态)的第1点应用
        /*

        Master master = new Master();
        Animal dog = new Dog("大黄",2,"土黄");
        Animal chick =  new Chick("母鸡",3,"花");
        Animal fish = new Fish("金鱼",1,"红");
        master.feed(dog);//在传参的同时, 系统自动做了向上造型
        master.feed(chick);
        master.feed(fish);

         */

        //演示向上造型
        /*

        Dog o1 = new Dog("大黄",2,"土黄");//狗是狗
        //o1.name/age/color/drink(/eat(/swimO/lookHome
        Animal o2 = new Dog("大黄",2,"土黄");//狗是动物----向上造型
        // o2.name/age/color/drink()/eat()
        Swim o3 = new Dog("大黄",2,"土黄");//狗会游泳-----向上造型
        //o3.swim();

         */
    }
}
